package com.one.rope.mvp.basecore.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName TimeBetweenUtils
 * @Description TODO
 * @Author gavin
 * @Date 2019-02-27 10:57
 * @Version 1.0
 **/
public class TimeBetweenUtils {

  public static final int YEAR_RETURN = 0;

  public static final int MONTH_RETURN = 1;

  public static final int DAY_RETURN = 2;

  public static final int HOUR_RETURN = 3;

  public static final int MINUTE_RETURN = 4;

  public static final int SECOND_RETURN = 5;

  public static long getBetween(Date beginDate, Date endDate, int returnPattern) {

    Calendar beginCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();
    beginCalendar.setTime(beginDate);
    endCalendar.setTime(endDate);
    switch (returnPattern) {
      case YEAR_RETURN:
        return getByField(beginCalendar, endCalendar, Calendar.YEAR);
      case MONTH_RETURN:
        return getByField(beginCalendar, endCalendar, Calendar.YEAR) * 12 + getByField(
            beginCalendar, endCalendar, Calendar.MONTH);
      case DAY_RETURN:
        return getTime(beginDate, endDate) / (24 * 60 * 60 * 1000);
      case HOUR_RETURN:
        return getTime(beginDate, endDate) / (60 * 60 * 1000);
      case MINUTE_RETURN:
        return getTime(beginDate, endDate) / (60 * 1000);
      case SECOND_RETURN:
        return getTime(beginDate, endDate) / 1000;
      default:
        return 0;
    }
  }

  private static long getByField(Calendar beginCalendar, Calendar endCalendar, int calendarField) {
    return endCalendar.get(calendarField) - beginCalendar.get(calendarField);
  }

  private static long getTime(Date beginDate, Date endDate) {
    return endDate.getTime() - beginDate.getTime();
  }

  /**
   * 计算日期距现在多久前 xx年xx月xx天xx小时xx分钟前
   */
  public static String calTimeAgo(Date date, int returnPattern) {
    Date nowDate = new Date();
    if (date.after(nowDate)) {
      return "0分钟前";
    }
    Calendar nowCal = Calendar.getInstance();
    nowCal.setTime(nowDate);
    Calendar dateCal = Calendar.getInstance();
    dateCal.setTime(date);

    int yearAgo = nowCal.get(Calendar.YEAR) - dateCal.get(Calendar.YEAR);
    int monthAgo = nowCal.get(Calendar.MONTH) - dateCal.get(Calendar.MONTH);
    if (monthAgo < 0) {
      yearAgo--;
      monthAgo += 12;
    }
    int dayAgo = nowCal.get(Calendar.DAY_OF_MONTH) - dateCal.get(Calendar.DAY_OF_MONTH);
    if (dayAgo < 0) {
      monthAgo--;
      dayAgo += dateCal.getActualMaximum(Calendar.DAY_OF_MONTH);
      if (monthAgo < 0) {
        yearAgo--;
        monthAgo += 12;
      }
    }
    int hourAgo = nowCal.get(Calendar.HOUR_OF_DAY) - dateCal.get(Calendar.HOUR_OF_DAY);
    if (hourAgo < 0) {
      dayAgo--;
      hourAgo += 24;
      if (dayAgo < 0) {
        monthAgo--;
        dayAgo += dateCal.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (monthAgo < 0) {
          yearAgo--;
          monthAgo += 12;
        }
      }
    }
    int minuteAgo = nowCal.get(Calendar.MINUTE) - dateCal.get(Calendar.MINUTE);
    if (minuteAgo < 0) {
      hourAgo--;
      minuteAgo += 60;
      if (hourAgo < 0) {
        dayAgo--;
        hourAgo += 24;
        if (dayAgo < 0) {
          monthAgo--;
          dayAgo += dateCal.getActualMaximum(Calendar.DAY_OF_MONTH);
          if (monthAgo < 0) {
            yearAgo--;
            monthAgo += 12;
          }
        }
      }
    }

    StringBuilder sb = new StringBuilder();
    switch (returnPattern) {
      case YEAR_RETURN:
        sb.append(yearAgo < 1 ? "" : (yearAgo + "年"));
        break;
      case MONTH_RETURN:
        sb.append(yearAgo < 1 ? "" : (yearAgo + "年"))
            .append(monthAgo < 1 ? "" : (monthAgo + "月"));
        break;
      case DAY_RETURN:
        sb.append(yearAgo < 1 ? "" : (yearAgo + "年"))
            .append(monthAgo < 1 ? "" : (monthAgo + "月"))
            .append(dayAgo < 1 ? "" : (dayAgo + "天"));
        break;
      case HOUR_RETURN:
        sb.append(yearAgo < 1 ? "" : (yearAgo + "年"))
            .append(monthAgo < 1 ? "" : (monthAgo + "月"))
            .append(dayAgo < 1 ? "" : (dayAgo + "天"))
            .append(hourAgo < 1 ? "" : (hourAgo + "小时"));
        break;
      case MINUTE_RETURN:
        sb.append(yearAgo < 1 ? "" : (yearAgo + "年"))
            .append(monthAgo < 1 ? "" : (monthAgo + "月"))
            .append(dayAgo < 1 ? "" : (dayAgo + "天"))
            .append(hourAgo < 1 ? "" : (hourAgo + "小时"))
            .append(minuteAgo < 1 ? "" : (minuteAgo + "分钟"));
        break;
      case SECOND_RETURN:
        sb.append(yearAgo < 1 ? "" : (yearAgo + "年"))
            .append(monthAgo < 1 ? "" : (monthAgo + "月"))
            .append(dayAgo < 1 ? "" : (dayAgo + "天"))
            .append(hourAgo < 1 ? "" : (hourAgo + "小时"))
            .append(minuteAgo < 1 ? "" : (minuteAgo + "分钟"));
        break;
      default:
        sb.append(yearAgo < 1 ? "" : (yearAgo + "年"))
            .append(monthAgo < 1 ? "" : (monthAgo + "月"))
            .append(dayAgo < 1 ? "" : (dayAgo + "天"))
            .append(hourAgo < 1 ? "" : (hourAgo + "小时"))
            .append(minuteAgo < 1 ? "" : (minuteAgo + "分钟"))
            .append("前");
        ;
        break;
    }

    return sb.toString();
  }

}
